import re

file_path = 'seasons_data_1949_2099.txt'


def find_year_data(year, file_path) -> list[tuple]:
    with open(file_path, 'r', encoding='utf-8') as file:
        content = file.read()

        # 正则表达式匹配特定年份的数据
        pattern = re.compile(rf"{year}: \[(.*?)\]", re.DOTALL)
        match = pattern.search(content)

        if match:
            # 提取括号内的所有内容
            data_str = match.group(1)
            # 去除字符串中的空白字符
            data_str = data_str.strip()
            # 将字符串分割成列表
            data_list = re.findall(r"\((\d+), (\d+), (\d+), (\d+)\)", data_str)
            # 转换为整数元组列表
            data_list = [(int(x[0]), int(x[1]), int(x[2]), int(x[3])) for x in data_list]
            return data_list
        else:
            return None


def extract_season_data(year_to_find: int) -> list[tuple]:
    data_for_year = find_year_data(year_to_find, file_path)
    if data_for_year:
        print(f"Data for year {year_to_find}:")
        print(data_for_year)
    else:
        print(f"No data found for year {year_to_find}.")
        return None


# 使用示例
if __name__ == '__main__':
    while True:
        year = input('输入年份:')
        if not year.isdigit():
            year = 2023
        year_to_find = int(year)
        data_for_year = find_year_data(year_to_find, file_path)

        if data_for_year:
            print(f"Data for year {year_to_find}:")
            print(data_for_year)
        else:
            print(f"No data found for year {year_to_find}.")
